package com.wss.common.widget;

import com.wss.common.base.ResourceTable;
import com.wss.common.utils.PxUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;

/**
 * Describe:数字进度条
 * Created by 吴天强 on 2018/11/1.
 */
public class NumberProgressBar extends Component implements Component.DrawTask {

    /**
     * 右侧未完成进度条的颜色
     */
    private static final int PAINT_START_COLOR = 0xffe5e5e5;

    /**
     * context
     */
    private Context context;

    /**
     * l
     * 主线程传过来进程 0 - 100
     */
    private int progress;

    /**
     * 得到自定义视图的宽度
     */
    private int viewWidth;


    private RectFloat pieOval;

    private RectFloat pieOvalIn;

    /**
     * 得到自定义视图的Y轴中心点
     */
    private int viewCenterY;

    /**
     * 已完成的画笔
     */
    private Paint paintInit = new Paint();


    /**
     * 未完成进度条画笔的属性
     */
    private Paint paintStart = new Paint();

    /**
     * 大圆的画笔
     */
    private Paint paintEndBig = new Paint();

    /**
     * 小圆的画笔
     */
    private Paint paintSmall = new Paint();


    /**
     * 画中间的百分比文字的画笔
     */
    private Paint paintText = new Paint();

    /**
     * 要画的文字的宽度
     */
    private int textWidth;

    /**
     * 画文字时底部的坐标
     */
    private float textBottomY;

    private int smallR;//小圆的半径
    private int bigR;//大圆半径
    private float radius;
    private int jR;//气泡矩形

    public NumberProgressBar(Context context) {
        this(context, null);
    }

    public NumberProgressBar(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * 文字总共移动的长度（即从0%到100%文字左侧移动的长度）
     */
//    private int totalMovedLength;
    public NumberProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        // 构造器中初始化数据
        smallR = PxUtils.vp2px(4);//小圆半径
        bigR = PxUtils.vp2px(8);//大圆半径
        radius = PxUtils.vp2px(10) / 2;//进度条高度
        jR = PxUtils.vp2px(6);//矩形

        initData();
        addDrawTask(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        // 未完成进度条画笔的属性
        paintStart.setColor(new Color(PAINT_START_COLOR));
        paintStart.setStrokeWidth(PxUtils.vp2px(1));
        paintStart.setDither(true);
        paintStart.setAntiAlias(true);
        paintStart.setStyle(Paint.Style.FILL_STYLE);

        // 已完成进度条画笔的属性
        try {
            int color = getResourceManager()
                    .getElement(ResourceTable.Color_theme)
                    .getColor();
            paintInit.setColor(new Color(color));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        paintInit.setStrokeWidth(PxUtils.vp2px(1));
        paintInit.setAntiAlias(true);
        paintInit.setDither(true);
        paintInit.setStyle(Paint.Style.FILL_STYLE);


        // 小圆画笔
        paintSmall.setColor(Color.WHITE);
        paintSmall.setAntiAlias(true);
        paintSmall.setStyle(Paint.Style.FILL_STYLE);

        // 大圆画笔
        try {
            int color = getResourceManager()
                    .getElement(ResourceTable.Color_theme)
                    .getColor();
            paintEndBig.setColor(new Color(color));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        paintEndBig.setAntiAlias(true);
        paintEndBig.setStyle(Paint.Style.FILL_STYLE);


        // 百分比文字画笔的属性
        int paintTextSizePx = PxUtils.fp2px(11);  //设置百分比文字的尺寸
        try {
            int color = getResourceManager()
                    .getElement(ResourceTable.Color_theme)
                    .getColor();
            paintText.setColor(new Color(color));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        paintText.setTextSize(paintTextSizePx);
        paintText.setAntiAlias(true);
        paintText.setFakeBoldText(true);

    }

    /**
     * @param progress 外部传进来的当前进度
     */
    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        //得到float型进度
        float progressFloat = progress / 100.0f;
        int viewHeight = getEstimatedHeight();//得到控件的高度

        viewWidth = getEstimatedWidth() - 4 * jR;

        viewCenterY = viewHeight - bigR;

        float currentMovedLen = viewWidth * progressFloat + 2 * jR;

        String str = progress + "%";

        Rect bounds = paintText.getTextBounds(str);
        textWidth = bounds.getWidth();
        textBottomY = bounds.getHeight();
        /*
         * 1：绘画的文本
         * 2.距离x的位移
         * 3.距离Y的位移
         * 4.画笔对象
         */
        canvas.drawText(paintText, str, currentMovedLen - textWidth / 2,
                viewCenterY - smallR / 2 - bigR / 2 - 2 * jR + textBottomY / 2);//文字


        //圆角矩形初始的
        canvas.drawRoundRect(new RectFloat(2 * jR, viewCenterY - radius, currentMovedLen,
                        viewCenterY + radius),
                radius, radius, paintInit);

        //圆角矩形--进行中
        canvas.drawRoundRect(new RectFloat(currentMovedLen, viewCenterY - radius, viewWidth + 2 * jR,
                viewCenterY + radius), radius, radius, paintStart);

        pieOval = new RectFloat(currentMovedLen - bigR, viewCenterY - bigR, currentMovedLen + bigR, viewCenterY + bigR);

        pieOvalIn = new RectFloat(currentMovedLen - smallR, viewCenterY - smallR, currentMovedLen + smallR, viewCenterY + smallR);

        //大圆
        canvas.drawArc(pieOval,
                new Arc(0, 360, true),
                paintEndBig);

        //小圆
        canvas.drawArc(pieOvalIn,
                new Arc(0, 360, true),
                paintSmall);
    }
}
